#include<iostream>
#include<vector>
#include<string>
#include<chrono>
#include<set>
#include<sw/redis++/redis++.h>
#include<thread>
#include"util.hpp"
using sw::redis::Redis;
using namespace std::chrono_literals;
using namespace std;

void test1(Redis& redis)
{
    cout<<"sadd and semembers"<<endl;
    redis.flushall();

    redis.sadd("key","111");

    redis.sadd("key",{"222","333","444"});

    set<string> elems = {"555","666","777"};

    redis.sadd("key",elems.begin(),elems.end());

    // 获取
    vector<string> results;
    auto it = back_inserter(results);
    redis.smembers("key",it);
    printContainer(results);

}

void test2(Redis& redis)
{
    cout<<"sismember"<<endl;
    redis.flushall();

    redis.sadd("key",{"111","222","333"});

    bool flag = redis.sismember("key","111");
    if(flag)
        cout<<"存在！"<<endl;
    else
        cout<<"不存在~"<<endl;

}

void test3(Redis& redis)
{
    cout<<"scard"<<endl; // 返回集合中元素的个数
    redis.flushall();

    redis.sadd("key",{"111","222","333"});
    long long result = redis.scard("key");
    cout<<result<<endl;
}

void test4(Redis& redis)
{
    cout<<"spop"<<endl;
    redis.flushall();

    redis.sadd("key",{"111","222","333"});
    auto res = redis.spop("key"); // 随机删除
    if(res)
        cout<<res.value()<<endl;
    else
        cout<<"error!"<<endl;
}

void test5(Redis& redis)
{
    cout<<"sinter 求交集"<<endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333"});
    redis.sadd("key2",{"111","444","555"});

    set<string> result;
    auto it = std::inserter(result,result.end());
    redis.sinter({"key1","key2"},it);
    printContainer(result);

}

void test6(Redis& redis)
{
    cout<<"sinterscore 存到另一个key中"<<endl;
    redis.flushall();
    
    redis.sadd("key1",{"111","222","333"});
    redis.sadd("key2",{"111","444","555"});

    long long length = redis.sinterstore("key3",{"key1","key2"});
    cout<<"len"<<length<<endl;

    set<string> result;
    auto it = std::inserter(result,result.end());
    redis.smembers("key3",it);
    printContainer(result);

}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    // test5(redis);
    test6(redis);

    return 0;
}